﻿using Stimulus360.Models.Cms;
using Stimulus360.Repositories;
using Stimulus360.Services.Interfaces;
using Stimulus360.Repositories.Interfaces;

namespace Stimulus360.Services
{
    /// <summary>
    ///     This class is used to interact with the ICmsRepository to perform operations on the project Photos
    ///     and Project Documents entities
    /// </summary>
    /// <remarks>
    ///     This class is invoked from the Project Service and not from the controller.. It will never be exposed as a REST service and does not
    ///     do any exception handling. It will simply throw exceptions to be handled appropriately by the Project
    ///     Service
    /// </remarks>
    public class CmsService : ICmsService
    {
        #region ICMSService Members

        /// <summary>
        ///     This class is used to interact with the ICMSRepository to perform
        ///     to perform logical and CRUD operations on the project Photos and Documents entities
        /// </summary>
        private readonly ICmsRepository _cmsRepository;

        /// <summary>
        ///     Default constructor. Creates an instance of CMSRepository
        /// </summary>
        public CmsService()
        {
                _cmsRepository = new CmsRepository();

        }

        /// <summary>
        ///     Dependency Injection constructor
        /// </summary>
        /// <param name="repository">
        ///     The ICMSRepository instance to be used.
        /// </param>
        public CmsService(ICmsRepository repository)
        {
                _cmsRepository = repository;

        }

        /// <summary>
        ///     Method to add a photo to the project details
        /// </summary>
        /// <param name="photo">
        ///     The photo to be added
        /// </param>
        public void AddPhoto(ProjectPhoto photo)
        {
                _cmsRepository.AddPhoto(photo);


        }

        /// <summary>
        ///     Method to retrieve photos for a given project
        /// </summary>
        /// <param name="projectId">
        ///     Project for which photos are to be retrieved
        /// </param>
        /// <param name="page">
        ///     Page number to be displayed
        /// </param>
        /// <param name="pageSize">
        ///     The number of photos to be displayed
        /// </param>
        /// <returns>
        ///     Photos for the given project
        /// </returns>
        public PaginatedCollection<ProjectPhoto> GetPhotos(int projectId, int? page, int pageSize)
        {
                var photoList = _cmsRepository.GetPhotos(projectId);
                return new PaginatedCollection<ProjectPhoto>(photoList, page.GetValueOrDefault(0), pageSize);
        }

        /// <summary>
        ///     Method to retrieve a photo by its ID
        /// </summary>
        /// <param name="photoId">
        ///     The Photo ID to be retrieved
        /// </param>
        /// <returns>
        ///     The Photo for the given ID
        /// </returns>
        public ProjectPhoto FindPhotoById(int photoId)
        {
                return _cmsRepository.FindPhotoById(photoId);

        }

        /// <summary>
        ///     Method to add a document to the given project
        /// </summary>
        /// <param name="document">
        ///     The document to be added
        /// </param>
        public void AddDocument(ProjectDocument document)
        {
                _cmsRepository.AddDocument(document);
        }

        /// <summary>
        ///     Method to retrieve documents for a given project
        /// </summary>
        /// <param name="projectId">
        ///     Project for which documents are to be retrieved
        /// </param>
        /// <param name="page">
        ///     Page number to be displayed
        /// </param>
        /// <param name="pageSize">
        ///     The number of documents to be displayed
        /// </param>
        /// <returns>
        ///     Documents for the given project
        /// </returns>
        public PaginatedCollection<ProjectDocument> GetDocuments(int projectId, int? page, int pageSize)
        {
                var docList = _cmsRepository.GetDocuments(projectId);
               return new PaginatedCollection<ProjectDocument>(docList, page.GetValueOrDefault(0), pageSize);
        }

        /// <summary>
        ///     Method to retrieve a document based on its ID
        /// </summary>
        /// <param name="docId">
        ///     ID for which document is to be retrieved
        /// </param>
        /// <returns>
        ///     Document  based on the given ID.
        /// </returns>
        public ProjectDocument FindDocumentById(int docId)
        {
                return _cmsRepository.FindDocumentById(docId);
       }
        #endregion
    }
}
